package com.ww.core.activity;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ProgressBar;

import com.ww.core.R;
import com.ww.core.util.DeviceUtil;
import com.ww.core.util.Logger;

/**
 * webView页面常用的功能，以供js调用.
 * @since 
 * @version 2012-9-12
 * @author wangw
 */
public class WebActivity extends MyActivity {
	private Context context = WebActivity.this;
	private WebView mWebView;
	private LinearLayout neterror;
	 private ProgressBar progressbar;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.core_webview);
		String title = getIntent().getStringExtra("title");
		mWebView = (WebView) findViewById(R.id.webview);
		progressbar= (ProgressBar) findViewById(R.id.progressbar);
		setTitle(title);

		neterror = (LinearLayout) findViewById(R.id.nonetwork);
		neterror.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				loadurl();
			}
		});

		try {
			Button back = (Button) findViewById(R.id.back);
			back.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					if (mWebView.canGoBack()) {
						mWebView.goBack();
					} else {
						finish();
					}
				}
			});
		} catch (Exception e) {
		}
		loadurl();
	}

	private void loadurl() {
		if (DeviceUtil.checkNet(context)) {
			neterror.setVisibility(View.GONE);
			String url = getIntent().getStringExtra("url");
			Logger.info("url:" + url);
			WebSettings webSetting = mWebView.getSettings();
			//设置js可用  
			webSetting.setJavaScriptEnabled(true);
			webSetting.setBuiltInZoomControls(false);
			webSetting.setLightTouchEnabled(true);
			webSetting.setSupportZoom(false);
			mWebView.setHapticFeedbackEnabled(false);
			mWebView.addJavascriptInterface(this, "android");

			//载入具体的web地址  
			mWebView.setVisibility(View.VISIBLE);
			mWebView.requestFocus();
			mWebView.loadUrl(url);
			mWebView.setWebViewClient(new HttpWebViewClient());
			mWebView.setWebChromeClient(new WebChromeClient());
		} else {
			neterror.setVisibility(View.VISIBLE);
			mWebView.setVisibility(View.GONE);
		}
	}

	//web视图客户端    
	public class HttpWebViewClient extends WebViewClient {
		public boolean shouldOverviewUrlLoading(WebView view, String url) {
			view.loadUrl(url);
			return true;
		}

		//开始加载  
		@Override
		public void onPageStarted(WebView view, String url, Bitmap favicon) {
			super.onPageStarted(view, url, favicon);
		}

		//结束加载  
		@Override
		public void onPageFinished(WebView view, String url) {
			super.onPageFinished(view, url);
		}
	}
	
	
    public class WebChromeClient extends android.webkit.WebChromeClient {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            if (newProgress == 100) {
                progressbar.setVisibility(View.GONE);
            } else {
                if (progressbar.getVisibility() == View.GONE)
                    progressbar.setVisibility(View.VISIBLE);
                progressbar.setProgress(newProgress);
            }
            super.onProgressChanged(view, newProgress);
        }
    }

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
			mWebView.goBack();
			return true;
		} else {
			finish();
		}
		return super.onKeyDown(keyCode, event);
	}
}
